import { calc } from 'a-calc/es';

export default {
  data() {
    return{
      tcSubtotal: '',
	  tcDanjia:'',
      form:{
        tcKm: '',
        tcCarType: '',
        tcIsUp: '',
        tcBasePrice: '',
        tcHomework: '',
        tcSubtotal: '',
      }
    }
  },
  watch:{
    // 拖车收费 - 公里数
    'form.tcKm'(val,_val){
      if(val<1&&val){
        this.form.tcKm = 1
      }else{
        this.form.tcKm = (val)
      }
      this.gettuocheFei()
    },
	// 拖车收费 - 车型
	'form.tcCarType'(){
	  this.gettuocheFei()
	},
    // 拖车收费 - 基价
    'form.tcBasePrice'(){
      this.gettuocheFei()
    },
	// // 拖车收费 - 作业费
	// 'form.tcHomework'(val){
	// 	console.log('tcDanjia',val);
	// 	!+val && (this.tcDanjia = "");
	//   this.gettuocheFei()
	// },
    // 拖车收费 - 上浮
    'form.tcIsUp'(){
      this.gettuocheFei()
    },
  },
  methods: {
    handleInput() {
      if(this.form.tcKm.length == 1) {
        this.form.tcKm = this.form.tcKm.replace(/[^1-9]/g, '');
      } else {
        this.form.tcKm = this.form.tcKm.replace(/\D/g, '');
      }
    },
    gettuocheFei(){
      let total = 0
      const up = this.form.tcIsUp === '是'?1.2:1
      if(!this.form.tcKm){
        total = ''
      }else if(this.form.tcKm<10){
        total = calc(`
                (${this.form.tcBasePrice || 0})
                * (${up})
              `)
		// 作业费
		this.form.tcHomework  = 0
      }else if(this.form.tcKm < 50){
        total = calc(`
                (${this.form.tcBasePrice || 0}
                + (${this.form.tcKm - 10 } *  ${this.tcDanjia || 0}) )
                * (${up})
              `)
		// 作业费
		this.form.tcHomework = calc(`
		( (${this.form.tcKm - 10 } *  ${this.tcDanjia || 0}) )
		`)
      }else if(this.form.tcKm>=50){
        total = calc(`
                (${this.form.tcBasePrice || 0}
                + ( 40 *  ${this.tcDanjia || 0}) )
                * (${up})
              `)
		// 作业费
		this.form.tcHomework =  calc(`
		(( 40 *  ${this.tcDanjia || 0}) )
		`)
      }
	  // 小计
      this.tcSubtotal = total
    },
	tcCancel(){
		if(!+this.tcSubtotal){
			this.form.tcCarType = ""
			this.form.tcKm = ""
			this.form.tcIsUp = ""
			this.form.tcBasePrice = ""
			this.tcDanjia = ""
			this.form.tcHomework = ""
		}
	}
  },
}
